package com.hm.demo1;

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeMap;

public class Demo1 {
    public static void main(String[] args) throws Exception {
        ArrayList<String> list = new ArrayList<>();

               BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream("D:/68期/Java进阶/课后作业/day10/hero.txt"),"GBK"));
            String s;
            int count=0;
            while(true){
               s = br.readLine();
               count++;
               if(s==null){
                   break;
               }
               //读取到字符串
               if(count!=1){
                   list.add(s);
               }
            }
//            String[] split = list.get(1).split(regex);
//            System.out.println(split.length);

        String regex="[,]+";
        ArrayList<Hero> heroes = new ArrayList<>();
        for (String o : list) {
            String[] split = o.split(regex);
            heroes.add(new Hero(split[0],Integer.parseInt(split[1]),Integer.parseInt(split[2]),Integer.parseInt(split[3])));

        }
        TreeMap<Hero, Double> map = new TreeMap<>(new Comparator<Hero>() {
            @Override
            public int compare(Hero o1, Hero o2) {
                if (o2.getSurvival() * 0.5 + o2.getAttack() * 0.2 + o2.getEffect() * 0.3 == o1.getSurvival() * 0.5 + o1.getAttack() * 0.2 + o1.getEffect() * 0.3) {
                    return Double.compare(o2.getSurvival() * 0.5 + o2.getAttack() * 0.2 + o2.getEffect() * 0.3, o1.getSurvival() * 0.5 + o1.getAttack() * 0.2 + o1.getEffect() * 0.3);
                } else {
                    //当
                    return o1.getName().compareTo(o2.getName());
                }
            }
        });
        //降序排列
        heroes.forEach(o->{
            map.put(o,o.getSurvival()*0.5+o.getAttack()*0.2+o.getEffect()*0.3);
        });
        System.out.println(map);
        //将结果写回到文件中
        PrintWriter pw = new PrintWriter("ccc.txt");
        pw.println("姓名 生存 攻击 效果 新手适应分");
        map.forEach((k,v)->{
            pw.println(k.getName()+","+k.getSurvival()+","+k.getAttack()+","+k.getEffect()+","+v);
                }
        );
        BufferedWriter bw= new BufferedWriter(new OutputStreamWriter (new FileOutputStream("D:/68期/Java进阶/课后作业/day10/hero.txt"),"GBK"));
        bw.write("姓名 生存 攻击 效果 新手适应分");
        bw.newLine();
        map.forEach((k,v)-> {
                    try {
                        bw.write(k.getName()+","+k.getSurvival()+","+k.getAttack()+","+k.getEffect()+","+v);
                        bw.newLine();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
                );
        pw.close();
        br.close();
        bw.close();
    }
}

